$(function(){
    $('#no_rooms').change(function(e){
        var v = $(e.target).val();
        setRooms(v); 
      //  enableChildAge();
    });

});


$(document).ready(function(){	
         setRooms($('#no_rooms').val());
         unsetRoons();
         enableAllChildAge();
});

function setRooms(v){
            try {
            switch(v) {
                case '1':
                   $('div[id^=room-]').css({'display':'none'});
                   $('#room-1').css({'display':'block'});
                break;
                case '2':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2').css({display:'block'});
                break;
                case '3':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2, #room-3').css({'display':'block'});
                break;
                case '4':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2, #room-3, #room-4').css({'display':'block'});
                break;
                case '5':
                    $('div[id^=room-]').css({'display':'none'});
                    $('#room-1, #room-2, #room-3, #room-4, #room-5').css({'display':'block'});
                break;
            }
        } catch(v) {}
}

function enableBookBotton(hotel_id, count){

   var control = 0;
   for (var i = 1; i <= count; i++){
          if( $('input[name=room_id_' + i +']:checked', '#hotel_book_id_' + hotel_id).val() == null  ) {
           control++;
       }
   }
   
   if (control == 0){
       $('#hotel_book_' + hotel_id).prop('disabled', false);
   } else {
       $('#hotel_book_' + hotel_id).prop('disabled', true); 
   }
   
      var formIds = [];
     // to get all forms
     $("form").each(function() {
          formIds.push(this.id);
     });
     // to call method to unchecked all radions from another forms
     var formId = 0;  
     for (var k = 0; i < formIds.length; k++){
         formId = formIds[k];
         formId = formId.substr(14);         
         if (formId != hotel_id){
             $('input[name=room_id_1]:checked', '#hotel_book_id_' + formId).prop('checked', false);
             $('input[name=room_id_2]:checked', '#hotel_book_id_' + formId).prop('checked', false);
             $('input[name=room_id_3]:checked', '#hotel_book_id_' + formId).prop('checked', false);
             $('#hotel_book_' + formId).prop('disabled', true); 
         }
     }   
   
     
  

}

function unsetRoons(){
    $('input[name=room_id_1]:checked').prop('checked', false);
    $('input[name=room_id_2]:checked').prop('checked', false);
    $('input[name=room_id_3]:checked').prop('checked', false);


}

function enableAgeByRoomAndChild(room, index, display){
    var name = '#' + room + index + '_div';
    $(name).css({'display': display});
    
}

function enable(div, indexValue){
      var name = 'div[id^=' + div + ']';
      $(name).css({'display':'none'});  
      var numChildren = 4;
      for (var i = 1; i <= numChildren; i++){
          if (i <= indexValue)
            enableAgeByRoomAndChild(div, i, 'block');
      }
}

//room 1

$(function(){
    $('#room_children_1').change(function(e){
        var v = $(e.target).val();
        if (v == 0){
            $('div[id^=room_children_1_age_]').css({'display':'none'});  
        }         
          else enable('room_children_1_age_', v);
    });

});

// room 2

$(function(){
    $('#room_children_2').change(function(e){
        var v = $(e.target).val();
        if (v == 0){
            $('div[id^=room_children_2_age_]').css({'display':'none'});  
        }         
          else enable('room_children_2_age_', v);
    });

});

// room 3
$(function(){
    $('#room_children_3').change(function(e){
        var v = $(e.target).val();
        if (v == 0){
            $('div[id^=room_children_3_age_]').css({'display':'none'});  
        }         
          else enable('room_children_3_age_', v);
    });

});



function enableAllChildAge(){
    var no_rooms = 3;
    for(var i = 1; i<= no_rooms; i++){
        var name_div = 'room_children_' + i + '_age_';
        var name_select = 'room_children_' + i;
        $(name_div).css({'display':'none'});              
        var no_children = $('#' + name_select).val();
        if (no_children == 0){
            $('div[id^='+ name_div +']').css({'display':'none'});  
        } else enable(name_div, no_children);
    }    
}
